package com.sss.gsb.ws.enpoints;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;

import com.sss.gsb.message.schemas.LoanRequest;
import com.sss.gsb.message.schemas.LoanResponse;
import com.sss.gsb.orm.model.Loan;
import com.sss.gsb.service.LoadParameter;
import com.sss.gsb.service.impl.LoanService;
import com.sss.gsb.util.SchemaConvertionUtils;

@Endpoint
public class CPKLoanEndpoint implements CPKEndpoint {
	private static final Logger log = LoggerFactory.getLogger(CPKLoanEndpoint.class);

	@Autowired
	private LoanService service;

	@PayloadRoot(localPart = "LoanRequest", namespace = NAMESPACE)
	public LoanResponse getLoan(LoanRequest request) {
		log.info("Loading loan endpoint...");
		try {
			LoadParameter param = SchemaConvertionUtils.toLoadParameter(request);
			List<Loan> result = this.service.loadData(param);
			LoanResponse response = new LoanResponse();
			for(Loan l: result){
				response.getLoan().add(SchemaConvertionUtils.toSchema(l));
			}
			return response; 
		} catch (Exception e) {
			log.error("Could not load data from endpoint :: [" + e.getMessage()
					+ "]", e);
		}
		return null;
	}
}
